﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GrassGrid : MonoBehaviour
{
    private GridLayout mGridLayout { get; set; }
    private List<GrassSlot> mGrassSlot { get; set; }

    private void Start()
    {
        mGridLayout = this.GetComponent<GridLayout>();
        mGrassSlot = new List<GrassSlot>();

        Image[] objTransforms = this.GetComponentsInChildren<Image>(true);
        foreach (var item in objTransforms)
        {
            GrassSlot slot = item.gameObject.GetComponent<GrassSlot>();
            if (slot == null)
            {
                slot = item.gameObject.AddComponent<GrassSlot>();
            }
            mGrassSlot.Add(slot);
        }
    }

    public void InitGrid()
    {
        foreach (var item in mGrassSlot)
        {
            item.SetCancelState();
        }
    }

    public void SelectSlot(Vector2 targetAnchorpos)
    {
        InitGrid();
        foreach (var item in mGrassSlot)
        {            
            if (item.GetAnchorPosition().x == targetAnchorpos.x|| item.GetAnchorPosition().y == targetAnchorpos.y)
            {
                //横向或纵向相等
                item.SetActState();
            }
        }
    }
}
